import { getAchievementList as fetchAchievementList, getAchievementDetail as fetchAchievementDetail, createAchievement as apiCreateAchievement, updateAchievement as apiUpdateAchievement, deleteAchievement as apiDeleteAchievement } from '@/api/achievement'

export default {
  namespaced: true,
  state: {
    achievementList: [],
    total: 0,
    currentAchievement: null,
    loading: false,
    filters: {
      categoryId: -1, // -1: 全部
      keyword: '',
      timeRange: [] // 时间范围
    },
    pagination: {
      page: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    SET_ACHIEVEMENT_LIST(state, list) {
      state.achievementList = list
    },
    ADD_ACHIEVEMENT_LIST(state, list) {
      state.achievementList = [...state.achievementList, ...list]
    },
    SET_TOTAL(state, total) {
      state.total = total
    },
    SET_CURRENT_ACHIEVEMENT(state, achievement) {
      state.currentAchievement = achievement
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_FILTERS(state, filters) {
      state.filters = { ...state.filters, ...filters }
    },
    SET_PAGINATION(state, pagination) {
      state.pagination = { ...state.pagination, ...pagination }
    },
    RESET_FILTERS(state) {
      state.filters = {
        categoryId: -1,
        keyword: '',
        timeRange: []
      }
      state.pagination.page = 1
    },
    UPDATE_ACHIEVEMENT_IN_LIST(state, updatedAchievement) {
      const index = state.achievementList.findIndex(item => item.id === updatedAchievement.id)
      if (index !== -1) {
        state.achievementList.splice(index, 1, updatedAchievement)
      }
    },
    REMOVE_ACHIEVEMENT_FROM_LIST(state, id) {
      const index = state.achievementList.findIndex(item => item.id === id)
      if (index !== -1) {
        state.achievementList.splice(index, 1)
      }
    }
  },
  actions: {
    // 获取成果列表
    async getAchievementList({ commit, state }) {
      commit('SET_LOADING', true)
      
      try {
        const params = {
          page: state.pagination.page,
          pageSize: state.pagination.pageSize
        }
        
        // 添加筛选条件
        if (state.filters.categoryId !== -1) {
          params.categoryId = state.filters.categoryId
        }
        
        if (state.filters.keyword) {
          params.keyword = state.filters.keyword
        }
        
        // 时间范围筛选
        if (state.filters.timeRange && state.filters.timeRange.length === 2) {
          params.startDate = state.filters.timeRange[0]
          params.endDate = state.filters.timeRange[1]
        }
        
        console.log('请求参数:', params)
        const res = await fetchAchievementList(params)
        
        if (res) {
          commit('SET_ACHIEVEMENT_LIST', res.records || [])
          commit('SET_TOTAL', res.total || 0)
          commit('SET_PAGINATION', { total: res.total || 0 })
        }
        return res
      } catch (error) {
        console.error('获取成果列表失败', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 加载更多成果
    async loadMore({ commit, state }) {
      if (state.loading) return
      
      commit('SET_LOADING', true)
      
      try {
        const nextPage = state.pagination.page + 1
        
        const params = {
          page: nextPage,
          pageSize: state.pagination.pageSize
        }
        
        // 添加筛选条件
        if (state.filters.categoryId !== -1) {
          params.categoryId = state.filters.categoryId
        }
        
        if (state.filters.keyword) {
          params.keyword = state.filters.keyword
        }
        
        // 时间范围筛选
        if (state.filters.timeRange && state.filters.timeRange.length === 2) {
          params.startDate = state.filters.timeRange[0]
          params.endDate = state.filters.timeRange[1]
        }
        
        const res = await fetchAchievementList(params)
        
        if (res) {
          commit('ADD_ACHIEVEMENT_LIST', res.records || [])
          commit('SET_PAGINATION', { page: nextPage, total: res.total || 0 })
        }
        return res
      } catch (error) {
        console.error('加载更多成果失败', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 获取成果详情
    async getAchievementDetail({ commit }, id) {
      commit('SET_LOADING', true)
      
      try {
        const res = await fetchAchievementDetail(id)
        if (res) {
          commit('SET_CURRENT_ACHIEVEMENT', res)
        }
        return res
      } catch (error) {
        console.error('获取成果详情失败', error)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 创建成果
    async createAchievement({ dispatch }, achievementData) {
      try {
        const res = await apiCreateAchievement(achievementData)
        return res
      } catch (error) {
        console.error('创建成果失败', error)
        throw error
      }
    },
    
    // 更新成果
    async updateAchievement({ commit, state }, achievementData) {
      try {
        const res = await apiUpdateAchievement(achievementData)
        if (res) {
          // 更新当前成果
          commit('SET_CURRENT_ACHIEVEMENT', { ...state.currentAchievement, ...achievementData })
        }
        return res
      } catch (error) {
        console.error('更新成果失败', error)
        throw error
      }
    },
    
    // 删除成果
    async deleteAchievement({ commit }, id) {
      try {
        const res = await apiDeleteAchievement(id)
        if (res) {
          commit('REMOVE_ACHIEVEMENT_FROM_LIST', id)
        }
        return res
      } catch (error) {
        console.error('删除成果失败', error)
        throw error
      }
    },
    
    // 设置筛选条件
    setFilters({ commit, dispatch }, filters) {
      commit('SET_FILTERS', filters)
      commit('SET_PAGINATION', { page: 1 })
      return dispatch('getAchievementList')
    },
    
    // 重置筛选条件
    resetFilters({ commit, dispatch }) {
      commit('RESET_FILTERS')
      return dispatch('getAchievementList')
    }
  },
  getters: {
    achievementList: state => state.achievementList,
    total: state => state.total,
    currentAchievement: state => state.currentAchievement,
    loading: state => state.loading,
    filters: state => state.filters,
    pagination: state => state.pagination,
    hasMore: state => state.achievementList.length < state.pagination.total
  }
} 